package edu.whu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.demo.domain.Operation;
import edu.whu.demo.dao.OperationDao;
import edu.whu.demo.service.IOperationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */
@Service
public class OperationServiceImpl extends ServiceImpl<OperationDao, Operation> implements IOperationService {

    @Autowired
    OperationDao operationDao;
    @Override
    public IPage<Operation> findOperation(Long id, Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        // 查找管理员操作记录
        // id标识是哪个管理员的
        Page<Operation> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Operation> qw = new QueryWrapper<>();
        qw.eq(condition.containsKey("admin_id"),"admin_id",id);
        //qw.eq(condition.containsKey("type"),"type",condition.get("type"));
        //实现复选
        if(condition.containsKey("type")) {
            String types = (String) condition.get("type");
            if (StringUtils.isNotBlank(types)) {
                //字符串转数组再转List
                Collection<String> collection = Arrays.asList(types.split(","));
                //in方法
                qw.in(collection.size() > 0, "type", collection);
            }
        }

        qw.gt(condition.containsKey("beginTime"),"time",condition.get("beginTime"));
        qw.le(condition.containsKey("endTime"),"time",condition.get("endTime"));

        //默认降序
        if(!condition.containsKey("order"))
            qw.orderByDesc("time");
        else qw.orderBy(condition.containsKey("order"), (Boolean) condition.get("order"),"time");

        operationDao.findOperationByWrapper(page,qw);

        return page;
    }

    @Override
    public Operation addOperation(Operation operation){
        operationDao.insert(operation);
        return operation;
    }
}
